package com.Ngbussiness.server;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.Ngbussiness.dao.usecouponDao;
import com.Ngbussiness.dao.impl.usecouponDaoImpl;
import com.Ngbussiness.util.Calenda;
import com.pingplusplus.Pingpp;
import com.pingplusplus.exception.PingppException;
import com.pingplusplus.model.Charge;


public class GetCharge extends HttpServlet {
	//String key = "sk_test_HqfrLOGyT840q5yLOGnvz5e5";
	String key = "sk_live_x57XzTEnbyfpiDgapc8DjqIs";
	String zhoubian_id = "app_zznjf9a9WzX19aTq";

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		// ��ȡ�����ͼƬ��id
		int price =Integer.parseInt(request.getParameter("price"));
		String userid = request.getParameter("id").toString();
		String ip = request.getParameter("ip").toString();
		// ip = java.net.URLDecoder.decode(ip, "utf-8");
		//ip = "127.0.0.1";
		// ip = DataTool.HexStringhtoString(ip);
		int amount = 100 * price;
		// ����Ҫ������һ���û���id,���ڴ�����ݿ����

		// �����һ���Զ����id�ľ���
		Calenda cal=new Calenda();
		long time=(long)cal.getNowTime().getTime();
		String id = ""+time;
		System.out.println("1111111111");
		usecouponDao uc=new usecouponDaoImpl();
		boolean b=uc.inpaynum(Integer.parseInt(userid),price,ip,id);
		Pingpp.apiKey = key;
		Map<String, Object> chargeMap = new HashMap<String, Object>();
		// ĳЩ������Ҫ���extra�������������ӿ��ĵ�
		chargeMap.put("amount", amount);
		chargeMap.put("currency", "cny");
		chargeMap.put("subject", "money");
		chargeMap.put("body", "充值");
		chargeMap.put("order_no", id);
		chargeMap.put("channel", "alipay");
		chargeMap.put("client_ip", ip);
		// chargeMap.put("client_ip", "127.0.0.1");
		Map<String, String> app = new HashMap<String, String>();
		app.put("id", zhoubian_id);
		chargeMap.put("app", app);
		try {
			// ����������
			Charge charge = Charge.create(chargeMap);
			System.out.println(charge);
			out.print(charge);
			out.flush();
			out.close();
		} catch (PingppException e) {
			e.printStackTrace();
		}

	}

	// public void retrieve(String id) {
	// try {
	// // ��ѯ���ʽ���
	// Charge charge = Charge.retrieve(id);
	// System.out.println(charge);
	//
	// } catch (PingppException e) {
	// e.printStackTrace();
	// }
	// }
}